using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace WpfAppListView
{
    public class TileView : ViewBase
    {
        private DataTemplate itemTemplate;
        public DataTemplate ItemTemplate
        {
            get { return itemTemplate; }
            set { itemTemplate = value; }
        }

        private Brush selectedBackground = Brushes.Transparent;
        public Brush SelectedBackground
        {
            get { return selectedBackground; }
            set { selectedBackground = value; }
        }

        private Brush selectedBorderBrush = Brushes.Black;
        public Brush SelectedBorderBrush
        {
            get { return selectedBorderBrush; }
            set { selectedBorderBrush = value; }
        }

        protected override object DefaultStyleKey
        {
            get { return new ComponentResourceKey(GetType(), "TileView"); }
        }

        protected override object ItemContainerDefaultStyleKey
        {
            get { return new ComponentResourceKey(GetType(), "TileViewItem"); }
        }       
    }


    public class ImagePathConverter : IValueConverter
    {
        private string imageDirectory = System.IO.Directory.GetCurrentDirectory();
        public string ImageDirectory
        {
            get { return imageDirectory; }
            set { imageDirectory = value; }
        }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string imagePath = System.IO.Path.Combine(ImageDirectory, (string)value);

            if (System.IO.File.Exists(imagePath))
            {
                //return new BitmapImage(new Uri(imagePath));

                BitmapImage bgImageSource = new BitmapImage();

                using (var stream = new System.IO.FileStream(imagePath, System.IO.FileMode.Open))
                {

                    try
                    {
                        bgImageSource.BeginInit();
                        bgImageSource.StreamSource = stream;
                        bgImageSource.CacheOption = BitmapCacheOption.OnLoad;
                        bgImageSource.EndInit();
                        bgImageSource.Freeze();
                    }
                    catch
                    {
                        return null;
                    }
                }

                return bgImageSource;
            }

            else
                return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            // throw new NotSupportedException("The method or operation is not implemented.");
            return null;
        }
    }
}
